package com.songcha.library_common.helper

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp

@Composable
fun <T> ComposeGridLayout(modifier: Modifier = Modifier, list:List<T>, rowCount:Int, horizontalSpace: Dp,verticalSpace: Dp, itemContent:@Composable (index:Int, item:T)->Unit) {
    Column(modifier = modifier){
        val totalRowCount = kotlin.math.ceil(list.size / rowCount * 1.0).toInt()
        for(i in 0 until totalRowCount) {
            val isLastRow = (i == totalRowCount -1)
            Row(
                modifier = Modifier.padding(bottom = if(isLastRow) 0.dp else verticalSpace)
                    .fillMaxWidth()
            ){
                for (j in 0 until rowCount) {
                    if(i * rowCount + j <= list.size- 1){
                        val item = list[i * rowCount + j]
                        Box(
                            modifier = Modifier.padding(end = if(j < rowCount - 1) horizontalSpace else 0.dp)
                                .weight(1f)
                        ){
                            itemContent(i * rowCount + j,item)
                        }

                    }
                }
            }
        }
    }


}

